package utility.time;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author  lit
 * @date 2018/3/20.
 */
public class ProcessTime {

    /**
     * 获取当前时间的字符串
     * @param format 需要的格式，如：yyyy-MM-dd HH:mm:ss或yyyy-MM-dd等
     * @return
     */
    public static String getCurrentTime(String format){
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取时间的年份
     * @param time 时间字符串
     * @return 返回年份值
     */
    public static int getYear(String time){
        return generateCalendar(time).get(Calendar.YEAR);
    }

    /**
     * 获取时间的月份
     * @param time
     * @return
     */
    public static int getMonth(String time){
        //Calendar类取出来的月份比实际值少1，所以需要额外加1
        return generateCalendar(time).get(Calendar.MONTH)+1;
    }

    /**
     * 获取几号
     * @param time
     * @return
     */
    public static int getDay(String time){
        return generateCalendar(time).get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取周几
     * @param time
     * @return
     */
    public static int getWeekday(String time){
        return generateCalendar(time).get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取小时数
     * @param time
     * @return
     */
    public static int getHour(String time){
        return generateCalendar(time).get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 字符串时间格式化为Calendar类型
     * @param time 时间字符串
     * @return
     */
    private static Calendar generateCalendar(String time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            if(time.length() > 10){
                date = sdf.parse(time);
            }else{
                date = sdf1.parse(time);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * 获取两个时间的分钟差
     * @param time1
     * @param time2
     * @return
     */
    public static long diffMinutes(String time1,String time2){
        long diffNum = -1;
        if(time1 != null && time1 != "" && time2 != null && time2 != ""){
            Calendar startTime = generateCalendar(time1);
            Calendar endTime = generateCalendar(time2);
            diffNum = Math.abs(startTime.getTimeInMillis() - endTime.getTimeInMillis())/(1000 * 60);

        }
        return diffNum;
    }

    /**
     * 判断两个时间前一个是否大于后一个.只判断时分秒
     * @param big 时分秒的时间
     * @param small 时分秒的时间
     * @return
     */
    public static boolean isBigger(String big,String small){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        //将字符串转换为date类型
        Date dt1 = null,dt2 = null;
        try {
            dt1 = sdf.parse(big);
            dt2 = sdf.parse(small);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime()>dt2.getTime()) {
            return true;
        }else{
            return false;
        }
    }


    public static String addDayTime(String time,int dayNum){
        if(time != null){
            Calendar calendar = generateCalendar(time);
            calendar.add(Calendar.DAY_OF_MONTH,dayNum);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return sdf.format(calendar.getTime());
        }
        return null;
    }
}
